package com.useeinfo.oa.open.Infrastructure.Interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.MD5Util;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

/**
 */
public class AppLoginCheckInteceptor extends HandlerInterceptorAdapter {

    @Autowired
    private SystemUserBiz systemUserBiz;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        /**
//         * 验证当前时间和配置文件时间间隔
//         */
//        String failureTimeStr = Configuration.getConfigurationByName("union");
//        Date failureTime = StringConverters.ToDateOnly(MD5Util.convertMD5(failureTimeStr));
//        if (failureTime.before(new Date())){
//            return false;
//        }

        if (null == SessionKeyUtils.getSessionUser(request)) {//用户未登录
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSON(ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE)));
            out.close();
            return false;
        }

        return true;
    }
}
